Passed
Pull Request — master (#436)
by
unknown
02:43
created

CreateNotificationCommandHandler.execute   A

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 19
rs 9.6
c 0
b 0
f 0
cc 2
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { Notification, NotificationType } from 'src/Domain/Notification/Notification.entity';
4
import { CreateNotificationCommand } from './CreateNotificationCommand';
5
import { INotificationRepository } from 'src/Domain/Notification/Repository/INotificationRepository';
6
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier';
7
8
const { MATTERMOST_CHANNEL_LEAVES_ID } = process.env;
9
10
@CommandHandler(CreateNotificationCommand)
11
export class CreateNotificationCommandHandler {
12
  constructor(
13
    @Inject('INotificationRepository')
14
    private readonly notificationRepository: INotificationRepository,
15
    @Inject('IMattermostNotifier')
16
    private readonly mattermostNotifier: IMattermostNotifier,
17
    ) {}
18
19
  public async execute(command: CreateNotificationCommand): Promise<string> {
20
    const { message, type, leaveReaquest } = command;
21
22
    if (type === NotificationType.POST) {
23
      const { id } = await this.mattermostNotifier.createPost(MATTERMOST_CHANNEL_LEAVES_ID, message);
24
      const notification = await this.notificationRepository.save(
25
        new Notification(
26
          type,
27
          message,
28
          id,
29
          leaveReaquest
30
        )
31
      );
32
33
      return notification.getId();
34
    }
35
36
    throw new Error('Type not managed');
37
  }
38
}
39